﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfSnqkGasAnalysis.IBusiness_impl;
using WpfSnqkGasAnalysis.Model;

namespace WpfSnqkGasAnalysis
{
    /// <summary>
    /// WindowWaterPointItemAdd.xaml 的交互逻辑
    /// </summary>
    public partial class WindowWaterPointItemAdd : Window
    {
        public WindowWaterPointItemAdd()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 主表id
        /// </summary>
        internal long mainKey = 0;

        //检查输入字符串是否为数字 //创建时间：2022-12-7 15:16:20，作者：王浩力
        private void numberCheck_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            var textElem = (sender as TextBox);

            //解决连续输入很多.
            if ((
                textElem.Text.StartsWith(".") || string.IsNullOrWhiteSpace(textElem.Text)
                || textElem.Text.Contains(".")
                ) && e.Text == ".")
            {
                e.Handled = true;
                return;
            }

            //禁止在0前面继续输入0
            int index = ((System.Windows.Controls.TextBox)e.Source).CaretIndex;
            if (textElem.Text.StartsWith("0.") && index == 0 && e.Text == "0")
            {
                e.Handled = true;
                return;
            }

            //不能输入非数字
            if (Regex.IsMatch(e.Text, @"[^\d+\.{0,1}\d+]+"))
            {
                e.Handled = true;
                return;
            }
        }

        //检查输入字符串是否为数字 //创建时间：2022-12-7 15:16:20，作者：王浩力
        private void numberCheck_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            TextBox textBox = sender as TextBox;
            if (!string.IsNullOrWhiteSpace(textBox.Text))
            {
                if (textBox.Text == "0.0" || textBox.Text.StartsWith("00"))
                {
                    textBox.Text = textBox.Text.Replace("00", "");
                }
                if (!textBox.Text.StartsWith("0.") && Regex.IsMatch(textBox.Text, @"0+[1-9]+\.*\d+", RegexOptions.IgnoreCase))
                {
                    textBox.Text = textBox.Text.TrimStart('0');
                }

                //输入的不是小数或正整数           
                Regex regex = new Regex(@"[^\d+\.{0,1}\d+]", RegexOptions.IgnoreCase);
                textBox.Text = regex.Replace(textBox.Text, "");
            }
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            Water_point_item point_Item = new Water_point_item();
            point_Item.InputTime = date_analytime.Text;
            if (txt_TestPressure.Text.ToMyDecimal().HasValue)
            {
                //保留两位小数。修改时间：2023-1-30 10:52:35
                point_Item.TestPressure = Math.Round(txt_TestPressure.Text.ToMyDecimal2(), 2);
            }

            point_Item.OPTestTemp = txt_OPTestTemp.Text;
            point_Item.OPWater = txt_OPWater.Text.ToMyDecimal();
            point_Item.TestPoint = txt_TestPoint.Text;
            point_Item.TestWater = txt_TestWater.Text.ToMyDecimal();
            point_Item.WaterPointId = mainKey;

            WaterPointBusiness bll = new WaterPointBusiness();
            var result = bll.AddItem(point_Item);
            if (result.Code == (int)ResultCode.Success)
            {
                //刷新列表数据
                RefreashListView?.Invoke(this, e);
            }
            ShowAlertHelpter.ShowAlert(result.Msg);

        }

        /// <summary>
        /// 刷新ListView数据
        /// </summary>
        public event EventHandler RefreashListView;


    }
}
